//
//  AppDelegate.swift
//  01weibo
//
//  Created by 李龙 on 15/6/23.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import AFNetworking
var shareAccount = account.loadAccount()
let HMSwitchRootVCNotification = "HMSwitchRootVCNotification"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setupAppearence()
        setupNetWork()
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultViewContriller()
        window?.makeKeyAndVisible()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: HMSwitchRootVCNotification, object: nil)
       // print(isNewUpdate())
        return true
    }
    func switchViewController(n:NSNotification)
    {
        let isMainVc = n.object as! Bool
        window?.rootViewController = isMainVc ? MainViewController() : welcomeViewController()
    }
    private func defaultViewContriller() ->UIViewController
    {
        if shareAccount != nil
        {
            return isNewUpdate() ? NewfeatureCollectionViewController() : welcomeViewController()
        }
        return MainViewController()
    }
    private func isNewUpdate() -> Bool
    {
//        let currentVision = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
//        let vision = NSNumberFormatter.numberFromString(currentVersion)!.doubleValue

        let version = NSNumberFormatter().numberFromString(currentVersion)!.doubleValue
        
        print(version)
        
        let versionkey = "versionkey"
        let sanBoxversion = NSUserDefaults.standardUserDefaults().doubleForKey(versionkey)
        
        print(sanBoxversion)
        
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: versionkey)
        
        return version > sanBoxversion

    }
    private func setupNetWork()
    {
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        let urlCache = NSURLCache(memoryCapacity: 4*1024*1024, diskCapacity: 20*1024*1024, diskPath: nil)
        NSURLCache.setSharedURLCache(urlCache)
    }
    func setupAppearence()
    {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

